// pluginMenu.cpp (c) 2011 JV Software
//

#include "FallenPlugin.h"

#define MAYA_GLOBAL_STRING( a, x ) a += "global string $"; \
								   a += x.ToAnsiPtr(); \
								   a += "Ctrl"; \
								   a += ";\n";

#define MAYA_LOCAL_STRING( a, x, y )  a += "string $";\
								      a += x;\
								      a += " = \"";\
								      a += y.ToAnsiPtr();\
									  a += "\";\n";

#define MAYA_SHOWMENU( a, x )		  a += "global string $gMainWindow; $"; \
									  a += x.ToAnsiPtr(); \
									  a += "Ctrl = `menu -p $gMainWindow -to true -l $name`"; \
									  a += ";\n";
									    

#define MAYA_ADDMENU_ITEM( a, x, y, z ) a += "menuItem -p $"; \
									 a += x.ToAnsiPtr(); \
									 a += "Ctrl -l \""; \
									 a += y.ToAnsiPtr(); \
									 a += "\" -command \""; \
									 a += z.ToAnsiPtr(); \
									 a += "\";\n"; \

FPluginMenu::FPluginMenu( FString menuName, int numItems, ... ) {
	va_list vl;

	Init( menuName );

	va_start(vl,numItems);
		for(int i = 0; i < numItems; i++ ) {
			FString name, cmd;

			name = va_arg(vl,wchar_t *);
			cmd = va_arg(vl,wchar_t *);

			AddMenuItem( name, cmd );
		}
	va_end( vl );

	Register();
}

/*
=============
FPluginMenu::Init
=============
*/
void FPluginMenu::Init( FString name ) {
	script = "";

	MAYA_GLOBAL_STRING( script, name );
	MAYA_LOCAL_STRING( script, "name", name );
	MAYA_SHOWMENU( script, name );
	menuname = name;
}

/*
=============
FPluginMenu::Register
=============
*/
void FPluginMenu::Register( void ) {
	MGlobal::executeCommand( script );
}

/*
=============
FPluginMenu::AddMenuItem
=============
*/
void FPluginMenu::AddMenuItem( FString name, FString action ) {
	MAYA_ADDMENU_ITEM( script, menuname, name, action );
}

/*
=============
FPluginMenu::UnloadMenu
=============
*/
void FPluginMenu::UnloadMenu( void ) {
	MString cmd;

	cmd = "deleteUI $";
	cmd += menuname.ToAnsiPtr();
	cmd += "Ctrl;\n";

	MGlobal::executeCommand( cmd );
}